﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace Dragon
{
    public partial class wfManageBA : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.fvBADetail.Visible = false;
                this.lblUpdateInfo.Visible = false;
            }            
        }

        protected void ddlProvince_DataBound(object sender, EventArgs e)
        {
            this.ddlProvince.Items.Insert(0, new ListItem("Tất cả", ""));
            this.ddlProvince.SelectedIndex = 0;
        }        

        protected void ddlBAS_DataBound(object sender, EventArgs e)
        {
            this.ddlBAS.Items.Insert(0, new ListItem("Tất cả", ""));
            this.ddlBAS.SelectedIndex = 0;
        }

        protected void ddlBAGroup_DataBound(object sender, EventArgs e)
        {
            this.ddlBAGroup.Items.Insert(0, new ListItem("Tất cả", ""));
            this.ddlBAGroup.SelectedIndex = 0;
        }

        protected void ddlBAStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ddlBAGroup.Items.Insert(0, new ListItem("Tất cả", ""));
            this.ddlBAGroup.SelectedIndex = 0;
        }

        protected void ddlBAStatus_DataBound(object sender, EventArgs e)
        {
            this.ddlBAStatus.Items.Insert(0, new ListItem("Tất cả", ""));
            this.ddlBAStatus.SelectedIndex = 0;
        }

        protected void ddlSMSStatus_DataBound(object sender, EventArgs e)
        {
            this.ddlSMSStatus.Items.Insert(0, new ListItem("Tất cả", ""));
            this.ddlSMSStatus.SelectedIndex = 0;
        }                   

        protected void txtFilter_TextChanged(object sender, EventArgs e)
        {
            FilterChanged();
        }

        protected void FilterChanged()
        {
            TextBox tb1 = (TextBox)this.GridView1.HeaderRow.FindControl("txtProvince");
            TextBox tb2 = (TextBox)this.GridView1.HeaderRow.FindControl("txtBAS");
            TextBox tb3 = (TextBox)this.GridView1.HeaderRow.FindControl("txtCodeNS");
            TextBox tb4 = (TextBox)this.GridView1.HeaderRow.FindControl("txtName");
            TextBox tb5 = (TextBox)this.GridView1.HeaderRow.FindControl("txtPhone");
            TextBox tb6 = (TextBox)this.GridView1.HeaderRow.FindControl("txtBAGroup");
            TextBox tb7 = (TextBox)this.GridView1.HeaderRow.FindControl("txtBAStatus");

            string tn1 = tb1.Text.Trim() == "" ? "|" : tb1.Text.Trim();
            string tn2 = tb2.Text.Trim() == "" ? "|" : tb2.Text.Trim();
            string tn3 = tb3.Text.Trim() == "" ? "|" : tb3.Text.Trim();
            string tn4 = tb4.Text.Trim() == "" ? "|" : tb4.Text.Trim();
            string tn5 = tb5.Text.Trim() == "" ? "|" : tb5.Text.Trim();
            string tn6 = tb6.Text.Trim() == "" ? "|" : tb6.Text.Trim();
            string tn7 = tb7.Text.Trim() == "" ? "|" : tb7.Text.Trim();

            string para1 = "ProvinceName";
            string para2 = "BASName";
            string para3 = "CodeNS";
            string para4 = "BAName";
            string para5 = "Phone";
            string para6 = "BAGroupName";
            string para7 = "BAStatusName";            

            sdsBAList.SelectParameters[para1].DefaultValue = tn1;
            sdsBAList.SelectParameters[para2].DefaultValue = tn2;
            sdsBAList.SelectParameters[para3].DefaultValue = tn3;
            sdsBAList.SelectParameters[para4].DefaultValue = tn4;
            sdsBAList.SelectParameters[para5].DefaultValue = tn5;
            sdsBAList.SelectParameters[para6].DefaultValue = tn6;
            sdsBAList.SelectParameters[para7].DefaultValue = tn7;
            this.GridView1.DataBind();

            tb1 = (TextBox)this.GridView1.HeaderRow.FindControl("txtProvince");
            tb2 = (TextBox)this.GridView1.HeaderRow.FindControl("txtBAS");
            tb3 = (TextBox)this.GridView1.HeaderRow.FindControl("txtCodeNS");
            tb4 = (TextBox)this.GridView1.HeaderRow.FindControl("txtName");
            tb5 = (TextBox)this.GridView1.HeaderRow.FindControl("txtPhone");
            tb6 = (TextBox)this.GridView1.HeaderRow.FindControl("txtBAGroup");
            tb7 = (TextBox)this.GridView1.HeaderRow.FindControl("txtBAStatus");

            if (tn1 != "|")
                tb1.Text = tn1;
            if (tn2 != "|")
                tb2.Text = tn2;
            if (tn3 != "|")
                tb3.Text = tn3;
            if (tn4 != "|")
                tb4.Text = tn4;
            if (tn5 != "|")
                tb5.Text = tn5;
            if (tn6 != "|")
                tb6.Text = tn6;
            if (tn7 != "|")
                tb7.Text = tn7;
        }

        protected void txtCustomer_TextChanged(object sender, EventArgs e)
        {
            string tn = this.txtCustomer.Text.Trim() == "" ? "|" : this.txtCustomer.Text.Trim();
            sdsBAList.SelectParameters["CustomerName"].DefaultValue = tn;
            this.GridView1.DataBind();            
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.GridView1.SelectedIndex > -1)
            {
                this.UpdatePanel2.Update();
                this.fvBADetail.Visible = true;
                this.lblUpdateInfo.Visible = true;
                DataView dv = (DataView)sdsSMSPeriod.Select(DataSourceSelectArguments.Empty);
                if (dv.Table.Rows[0][0].ToString().Trim() != "")
                {
                    DateTime fromDate = Convert.ToDateTime(dv.Table.Rows[0][0].ToString());
                    DateTime toDate = Convert.ToDateTime(dv.Table.Rows[0][1].ToString());
                    this.lblUpdateInfo.Text = "BA nhắn tin từ ngày " + fromDate.ToString("MM/dd/yyyy") + " đến ngày " + toDate.ToString("MM/dd/yyyy");
                }
                else
                    this.lblUpdateInfo.Text = "BA chưa nhắn tin sản lượng!";
            }
            else
            {
                this.fvBADetail.Visible = false;
                this.lblUpdateInfo.Visible = false;
            }
        }

        protected void fvBADetail_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
        {
            if (e.AffectedRows > 0)
            {
                this.lblUpdateInfo.Text = "Cập nhật thành công!";
                FilterChanged();       
                this.UpdatePanel1.Update();                
            }
            else
                this.lblUpdateInfo.Text = "Cập nhật không thành công. Kiểm tra lại dữ liệu nhập!";
        }

        protected void sdsBAList_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {            
            int sodong = e.AffectedRows;
            lblInfo.Text = "Có tất cả " + sodong.ToString() + " nhân viên trong danh sách";
            if (sodong <= 1)
            {
                this.GridView1.SelectRow(-1);
            }
        } 

        protected void ExportData_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/wfBAList.aspx");            
        }                 
    }
}
